package com.harmontronics.hanma.paas.paas.controller;

import cn.hutool.core.io.FileTypeUtil;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import com.harmontronics.hanma.paas.paas.consts.BaseI18nKeyEnum;
import com.harmontronics.hanma.paas.paas.consts.FileTypeConstants;
import com.harmontronics.hanma.paas.paas.exception.ErrorKeyException;
import com.harmontronics.hanma.paas.paas.utils.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/images")
@Api(tags = {"上传-图片"})
public class ImagesController {

    @Value("${local.imgfile.prefix}")
    private String filePrefix;

    @Value("${local.imgfile.url}")
    private String url;

    @Value("${local.imgfile.localWinAddress}")
    private String localWinAddress;

    @Value("${local.imgfile.localLinuxAddress}")
    private String localLinuxAddress;

    @PostMapping("/upload")
    @ApiOperation("上传图片")
    public ResponseEntity<List> singleImageUpload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            throw new ErrorKeyException(BaseI18nKeyEnum.REQUEST_PARAM_ERROR);
        }

//        Long fileSize = file.getSize();
//        Boolean result = FileUtil.checkFileSize(fileSize,10,"M");
//        if(!result){
//            throw new ErrorKeyException(BaseI18nKeyEnum.FILESIZE_LONG);
//        }
//
//        String fileType;
//        try{
//            fileType = FileTypeUtil.getType(file.getInputStream());
//            if(!fileType.equalsIgnoreCase(FileTypeConstants.FILE_JPG) && !fileType.equalsIgnoreCase(FileTypeConstants.FILE_PNG)){
//                throw new ErrorKeyException(BaseI18nKeyEnum.REQUEST_PARAM_ERROR);
//            }
//        }catch(Exception e){
//            throw new ErrorKeyException(BaseI18nKeyEnum.REQUEST_PARAM_ERROR);
//        }

        String localAddress = "";

        if(System.getProperty("os.name").toLowerCase().contains("windows")) {
            localAddress = localWinAddress;
        }else{
            //若当前系统是linux系统
            localAddress = localLinuxAddress;
        }

        //如果文件夹不存在，则创建
        File targetFile = new File(localAddress);
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }
        long uploadTime = System.currentTimeMillis();
        String filename = uploadTime + file.getOriginalFilename();
        try {
            byte[] bytes = file.getBytes();

            Path path = Paths.get(localAddress + filename);
            Files.write(path, bytes);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ErrorKeyException(BaseI18nKeyEnum.REQUEST_PARAM_ERROR);
        }
        String realFilePrefix = filePrefix.substring(0, filePrefix.indexOf("/", 1));
        List<String> urlList = new ArrayList<>();
        String extUrl = realFilePrefix + File.separator + filename;
        urlList.add(extUrl);
        return ResponseEntity.ok(urlList);
    }
}
